﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using ChuongtrinhTracnghiem;

namespace ChuongtrinhTracnghiem
{
    public partial class FormThi : Form
    {
        public FormThi()
        {
            InitializeComponent();
        }
        int socau = FormThongtin.Socau;
        int ii=0;
        int Thoigian = Convert.ToInt32(FormThongtin.Socau.ToString()) * 30;
        
        public DataTable ds = new DataTable();

        public int caudung=0;
        public int[] mangcauhoi = new int[100];
        public char[] mangtraloi = new char[100];
        public char[] mangdapan = new char[100];

        Model.Test kt = new ChuongtrinhTracnghiem.Model.Test();
        
        private void Taocau(int x)
        {
            listBox_Cauhoi.Items.Clear();
            for (int i = 1; i <= x; i++)
            {
                listBox_Cauhoi.Items.Add("Câu " + i);
            }
        }

        private void listBox_Cauhoi_SelectedIndexChanged(object sender, EventArgs e)
        {
            radioButton_A.Visible = true;
            radioButton_B.Visible = true;
            radioButton_C.Visible = true;
            radioButton_D.Visible = true;
            button_Kq.Visible = true;
      
            ii = listBox_Cauhoi.SelectedIndex;

            richTextBox_ID.Text = ds.Rows[mangcauhoi[ii]]["CauHoiID"].ToString();
            richTextBox_Cauhoi.Text = ds.Rows[mangcauhoi[ii]]["CauHoi"].ToString();
            richTextBox_DapanA.Text = "a/ " + ds.Rows[mangcauhoi[ii]]["DapAnA"].ToString();
            richTextBox_DapanB.Text = "b/ " + ds.Rows[mangcauhoi[ii]]["DapAnB"].ToString();
            richTextBox_DapanC.Text = "c/ " + ds.Rows[mangcauhoi[ii]]["DapAnC"].ToString();
            richTextBox_DapanD.Text = "d/ " + ds.Rows[mangcauhoi[ii]]["DapAnD"].ToString();

            radioButton_A.Checked = false;
            radioButton_B.Checked = false;
            radioButton_C.Checked = false;
            radioButton_D.Checked = false;

            switch (mangtraloi[ii])
            {
                case 'A': { radioButton_A.Checked = true; break; }
                case 'B': { radioButton_B.Checked = true; break; }
                case 'C': { radioButton_C.Checked = true; break; }
                case 'D': { radioButton_D.Checked = true; break; }
            }

            Dapan(ii); //Hiển kết quả và đáp án từng câu
        }

        private void FormThi_Load(object sender, EventArgs e)
        {

            string s = "select * from TblCauhoi";
            Model.Laydulieu dl = new Model.Laydulieu();
            ds = dl.Dulieu(s);
            label_kq.Visible = false;
            label_thoigian.Text = kt.ThoigianTest(Thoigian);
            label_thoigian.ForeColor = Color.Red;
            listBox_Cauhoi.Visible = false;
            radioButton_A.Visible = false;
            radioButton_B.Visible = false;
            radioButton_C.Visible = false;
            radioButton_D.Visible = false;
            button_Kq.Visible = false;

            label_Hoten.Text = FormThongtin.Hoten;
            label_Hoten.ForeColor = Color.Blue;

            Taocau(socau);

            listBox_Cauhoi.SelectedIndex = 0;

            mangcauhoi = kt.Taomangcauhoi(socau);

            mangtraloi = kt.Khoitaomangtraloi(socau);

            mangdapan = kt.Taomangdapan(socau, ds, mangcauhoi);

            listBox_Cauhoi.Visible = true;

            //Thanh Bar

            progressBar_Thoigian.Maximum = Thoigian;
            progressBar_Thoigian.Value = 0;
            progressBar_Thoigian.Step = 1;
            timer1.Interval = 1000;
            timer1.Start();

            // MessageBox.Show("vao day khong");
            Model.Ketnoi.Boketnoi();
        }

        private void button_Kq_Click(object sender, EventArgs e)
        {
            label_kq.Visible = true;
            radioButton_A.Enabled = false;
            radioButton_B.Enabled = false;
            radioButton_C.Enabled = false;
            radioButton_D.Enabled = false;
            Dapan(ii);
            button_Kq.Enabled = false;
            CapnhatTest();
            label_diem.Text = kt.Chamdiem(socau, mangtraloi, mangdapan) + " đúng/" + socau + " câu";
            label_diem.ForeColor = Color.Blue;
  
        }
         private void Dapan(int i)
        {
            if (mangtraloi[i] == mangdapan[i])
            {
                label_kq.Text = "Đúng";
                label_kq.ForeColor = Color.Blue;
            }
            else
            {
                label_kq.Text = "Sai. " + "Đáp án đúng là " + mangdapan[i];
                label_kq.ForeColor = Color.Red;
            }   
        }

        private void radioButton_A_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_A.Checked == true)
                mangtraloi[ii] = 'A';
        }

        private void radioButton_B_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_B.Checked == true)
                mangtraloi[ii]='B';

        }

        private void radioButton_C_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_C.Checked == true)
                mangtraloi[ii] = 'C';

        }

        private void radioButton_D_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_D.Checked == true)
                mangtraloi[ii] = 'D';

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (button_Kq.Enabled == false)
                timer1.Stop();
            else
            {
                progressBar_Thoigian.PerformStep();
                label_thoigian.Text = kt.ThoigianTest(progressBar_Thoigian.Maximum - progressBar_Thoigian.Value);
                if (progressBar_Thoigian.Value == progressBar_Thoigian.Maximum)
                {
                    timer1.Stop();
                    label_kq.Visible = true;
                    radioButton_A.Enabled = false;
                    radioButton_B.Enabled = false;
                    radioButton_C.Enabled = false;
                    radioButton_D.Enabled = false;
                    label_diem.Text = kt.Chamdiem(socau, mangtraloi, mangdapan) + " đúng/" + socau + " câu";
                    label_diem.ForeColor = Color.Blue;
                    Dapan(ii);
                    button_Kq.Enabled = false;
                    CapnhatTest();
                }
            }
        }
      
        private void CapnhatTest()
        {
            string ID = DateTime.Now.ToString();
            string UserID = FormThongtin.UserID;
            int Socau = socau;
            int Traloidung = kt.Chamdiem(socau, mangtraloi, mangdapan);
        
            
            //Model.TruyvanSQL sql1 = new Model.TruyvanSQL();
            //sql1.SQL(s1);
            SqlConnection con = new SqlConnection();
            con.ConnectionString = (@"Data Source=(local);Initial Catalog=TracnghiemData;Integrated Security=True");

            try
            {
                string s1 = "INSERT INTO TblKiemtra(UserID,Socau,Traloidung) VALUES('"+UserID+"','"+Socau+"','"+Traloidung+"')";
                SqlCommand cmd3 = new SqlCommand();
                cmd3.Connection = con;
                cmd3.CommandText = s1;
                con.Open();
                cmd3.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception x)
            {
               MessageBox.Show("loi~");
            }
            for (int i = 0; i < socau; i++)
            {
                string s2 = "INSERT INTO TblChitietKiemtra(ID, CauhoiID, TraLoi) VALUES('" + UserID + "','" + ds.Rows[mangcauhoi[i]]["CauHoiID"].ToString() + "','" + mangtraloi[i] + "')";
                SqlCommand cmd4 = new SqlCommand();
                cmd4.Connection = con;
                cmd4.CommandText = s2;
                try
                {
                    con.Open();
                    cmd4.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception x)
                {
                    MessageBox.Show("loi~");
                }
            }
        }

        
    }
}
